package com.bmslz.monitor.service.impl;

import com.bmslz.monitor.base.CommonConstant;
import com.bmslz.monitor.service.RuleDataCompare;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

@Component
public class RuleDataCompareDateTime implements RuleDataCompare {

    @Override
    public boolean supports(String dataType) {
        return CommonConstant.ATT_KEY_TYPE_DATA_TIME.equals(dataType);
    }


    @Override
    public boolean compare(String condition, String modelValue, String attValue) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        try {
            LocalDateTime modelDateTime = LocalDateTime.parse(modelValue, formatter);
            LocalDateTime attDateTime = LocalDateTime.parse(attValue, formatter);

            // 使用 compareTo 进行比较
            int comparisonResult = attDateTime.compareTo(modelDateTime);

            return switch (condition) {
                case CommonConstant.ALARM_RULE_COMPARE_CONDITION_GT -> comparisonResult > 0;
                case CommonConstant.ALARM_RULE_COMPARE_CONDITION_GE -> comparisonResult >= 0;
                case CommonConstant.ALARM_RULE_COMPARE_CONDITION_EQ -> comparisonResult == 0;
                case CommonConstant.ALARM_RULE_COMPARE_CONDITION_LT -> comparisonResult < 0;
                case CommonConstant.ALARM_RULE_COMPARE_CONDITION_LE -> comparisonResult <= 0;
                default -> false;
            };
        } catch (DateTimeParseException e) {
            e.printStackTrace();
            return false;
        }
    }
}
